﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Process
{
    public partial class ManageBlock : BasePage
    {
        private Lc_BlockBLL lbb = new Lc_BlockBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string dep = string.Empty;
                if (Request.QueryString.Count > 0)
                {
                    dep = Request.QueryString["ID"];
                }
                BindListBox(dep);
            }
        }

        private void BindListBox(string dels)
        {
            string[] dep = null;
            if (dels.Length != 0)
            {
                dep = dels.Split(',');
            }
            else
            {
                dep = new string[0];
            }
            DataTable dt = lbb.GetAllInfo("", "");
            foreach (string id in dep)
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (id == row["N_BLOCK_ID"].ToString())
                    {
                        ListItem item = new ListItem();
                        item.Text = row["C_BLOCK_NAME"].ToString();
                        item.Value = row["N_BLOCK_ID"].ToString();
                        ListBox.Items.Add(item);
                    }
                }
            }

            foreach (DataRow Row in dt.Rows)
            {
                bool isva = false;
                foreach (string id in dep)
                {
                    if (Row["N_BLOCK_ID"].ToString() == id)
                    {
                        isva = true;
                        break;
                    }
                }
                if (isva)
                    continue;
                ListItem item1 = new ListItem();
                item1.Text = Row["C_BLOCK_NAME"].ToString();
                item1.Value = Row["N_BLOCK_ID"].ToString();
                ListBoxBOCKM.Items.Add(item1);
            }
        }

        protected void BtnRight_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ListBoxBOCKM.SelectedValue))
            {
                ListItem item = ListBoxBOCKM.SelectedItem;
                ListBox.Items.Add(item);
                ListBoxBOCKM.Items.RemoveAt(ListBoxBOCKM.SelectedIndex);
            }
        }

        protected void BtnLeft_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ListBox.SelectedValue))
            {
                ListItem item = ListBox.SelectedItem;
                ListBoxBOCKM.Items.Add(item);
                ListBox.Items.RemoveAt(ListBox.SelectedIndex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string block = "";
            if (ListBox.Items.Count != 0)
            {
                string name = "";
                string id = "";
                foreach (ListItem item in ListBox.Items)
                {
                    name += item.Text + ",";
                    id += item.Value + ",";
                }
                name = name.Substring(0, name.Length - 1);
                id = id.Substring(0, id.Length - 1);
                block = name + "%" + id;
            }
            else
            {
                block = "" + "%" + "";
            }
            MessageBox.ResponseScript(this, "FORM_SUBMIT(\"" + block + "\")");
            //Response.Write("<script> window.returnValue='" + block + "';window.opener=null;window.close();</script>");
        }

        protected void btnXS_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ListBox.SelectedValue))
            {
                int index = ListBox.SelectedIndex;
                if (index > 0)
                {
                    ListItem item = ListBox.SelectedItem;
                    ListBox.Items.RemoveAt(ListBox.SelectedIndex);
                    ListBox.Items.Insert(index - 1, item);
                }
            }
        }

        protected void btnXX_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ListBox.SelectedValue))
            {
                int index = ListBox.SelectedIndex;
                if (index < ListBox.Items.Count - 1)
                {
                    ListItem item = ListBox.SelectedItem;
                    ListBox.Items.RemoveAt(ListBox.SelectedIndex);
                    ListBox.Items.Insert(index + 1, item);
                }
            }
        }
    }
}